<?php
namespace app\admin\controller;
use app\admin\model\AuthAssignment;
use \app\admin\model\User;
use \app\admin\controller\BaseController;
use \think\Db;
use app\admin\model\UserAddr;
class Users extends BaseController
{
	public function index(User $user)
	{
		$this->isLogin();
		$result = $user->order('create_time desc')->paginate(10); //分页
		$page = $result->render();
        $list['all'] = $user->count();
        $list['is_use'] = $user->where('is_use = 0')->count();
        $list['is_no_use'] = $user->where('is_use = 1')->count();
        $type = 0;
		return view('users/index', compact('result', 'page', 'list', 'type'));
	}
	public function select(User $user)
	{
		$this->isLogin();
		$result = $user->where('username', 'like', input('post.username'))->paginate(10); //分页
		$page = $result->render();
        $list['all'] = $user->count();  //所有用户的数量
        $list['is_use'] = $user->where('is_use = 0')->count(); //可用用户的数量
        $list['is_no_use'] = $user->where('is_use = 1')->count(); //禁用用户的数量
		$type = input('type');
		return view('users/index', compact('result', 'page', 'list', 'get','type'));
	}
	/**
	 * 禁用用户
	 * @param  [type] $user_id [description]
	 * @param  [type] $is_use  [description]
	 * @return [type]          [description]
	 */
	public function delete($user_id, $is_use)
	{
		$this->isLogin();
		$user = new User();
		if($is_use == '0') {
			$result = $user->where(['id' => $user_id])->update(['is_use' => 1]);
			if($user){
				$this->redirect('users/index');
			} else {
				$this->error('操作失败，请联系管理员');
			}

		} else {
			$result = $user->where(['id' => $user_id])->update(['is_use' => 0]);
			if($user){
				$this->redirect('users/index');
			} else {
				$this->error('操作失败，请联系管理员');
			}
		}
	}
	/**
	 * 添加用户
	 */
	public function insert(User $user,AuthAssignment $assignment)
	{
		$this->isLogin();
		$username = input('username');
		if(strlen($username) != 11) {
			return $this->error('请填写正确的手机号');
		}
        $pwd = input('pwd');
		$post['username'] = $username;
		$post['password'] = md5($pwd);
		$post['iphone'] = $username;
		$post['name'] = '钟鸣'.$username;
		$post['create_time'] = time();
		$post['is_use'] = 0;
		$post['rank'] = 0;
		$post['ip'] = $_SERVER['REMOTE_ADDR'];
		$result = $user->allowField(true)->save($post);
		$item['item_name'] = 'staff';
		$item['user_id'] = $user->getLastInsID();
		$item['created_at'] = time();
        $recode = $assignment->allowField(true)->save($item);
		if($result && $recode) {
				$this->success('用户添加成功','users/index');

			}else  {
				$this->error('添加商品失败，请联系管理员');
			}
	}
	/**
	 * 用户信息编辑
	 */
	public function update(User $user)
	{
		$this->isLogin();
		$username = input('post.username');
		$name = input('post.name');
		$post = [];
		if(input('post.name')) {
			$post['name'] = input('post.name');
		} 
		if(input('post.beans')) {
			$post['beans'] = input('post.beans');
		}
		if(!$post) {
			return $this->error('参数不能为空');
		}
		
		$result = $user->where('username','=',$username)->update($post);
		if ($result) {
			$this->success('修改成功');
		} else {
			$this->error('修改失败，请联系管理员');
		}
	}
	public function updatePassword(User $user)
	{
		$this->isLogin();
		$username = input('post.username');
		$pwd = input('post.pwd');
		$result = $user->where('username','=',$username)->update(['password' => md5($pwd)]);
		if ($result) {
			$this->success('修改成功', 'users/index');
		} else {
			$this->error('修改失败，请联系管理员');
		}
	}
	/**
	 * 禁用  使用 全部
	 * @param  User   $user [description]
	 * @return [type]       [description]
	 */
	public function indexs(User $user,$type)
	{
		$this->isLogin();
		if(empty($type)) {
			$result = $user->paginate(10); //分页
			$page = $result->render();
			$type = 0;

		} else if($type == '1') {
			$result = $user->where('is_use = 0')->paginate(10); //
			$page = $result->render();
            $type = 1;
		} else if($type == '2') {
			$result = $user->where('is_use = 1')->paginate(10);
			$page = $result->render();
            $type = 2;
		}
		$list['all'] = $user->count();
		$list['is_use'] = $user->where('is_use = 0')->count();
		$list['is_no_use'] = $user->where('is_use = 1')->count();
		return view('users/index', compact('result', 'page', 'list', 'type'));
	}
	public function details(User $user,UserAddr $userAddr)
	{
		$this->isLogin();
        $user_id = input('user_id');
        $users = $user->where('id',$user_id)->find();
        $userAddrs = $userAddr->where('user_id',$user_id)->select();

	    return view('users/userdesc',compact('users','userAddrs'));
    }
    /**
     * 订单备注
     * @param  User $user [description]
     * @return [type]                  [description]
     */
    public function userRemarks(User $user)
    {
    	$this->isLogin();
        if(!input('remarks') && !input('username')) {
            return $this>setError('参数不能为空');
        }
        $post['remarks'] = input('remarks');

        $result = $user->where(['username' => input('username')])->update($post);
        if(!$result) {
            return $this->setError('添加备注失败');
        }
        return $this->setSuccess('添加备注成功');
    }

}